import os

def findFile(root_dir,filetype,method='walk'):
    if method == 'walk':
        for root, dirs, files in os.walk(root_dir):
            for file in files:
                if os.path.splitext(file)[1] == filetype:
                    all_files.append(os.path.join(root,file))
    if method == 'listdir':
        dirs = os.listdir(root_dir)
        for dir in dirs:
            if os.path.splitext(dir)[1] == filetype:
                all_files.append(os.path.join(root_dir,dir))
            elif (os.path.splitext(dir)[1] == '') & (os.path.splitext(dir)[0][0] != '.') :
                child_dir = os.path.join(root_dir,dir)
                findFile(child_dir,filetype,method='listdir')


if __name__ == "__main__":
    all_files = []
    current_dir = 'E:\\深度学习\\machine_learning\\machinelearning_homework\\homework_01_python'
    target_filetype = '.py'
    findFile(current_dir,target_filetype,method='listdir')
    if os.path.exists('search_resualts.txt'):
        os.remove('search_resualts.txt')
    with open('search_resualts.txt','a') as f:
        for file in all_files:
            f.write(file+'\n')
